// Data  Service Manage

// TODO need config
var config = {
    dsm_restful_ip: "0.0.0.0",
    dsm_restful_port: 6099,
};
var RestfulServer = require('../sdk/restful-server');
var dsm_api_server = new RestfulServer(config.dsm_restful_api, config.dsm_restful_port);
var db_ops = require('./db_op.js');
db_ops.create_project({"email": "abc@112.com",
"name":"test",
"description":"test aaa",
"role_id":"123dd",
"passwd": "dddd"})
// const
var TAG = "DSM-RESTFULL: ";

function dsm_restful_server_start(server) {
    server.on("started", function() {
        console.log(TAG + "dsm resetful server started.");
    });

    server.registerService("/api/project/:id", "get", function(req, res) {
        var id = req.params.id;
        console.log(TAG + 'project id ' + id);
    });

    // create project
    server.registerService("/api/project/", "post", function(req, res) {
        var pro = JSON.stringify(req.body);
        console.log(TAG + pro)
        // check projects
        db_ops.
            create_project(pro)
            .then(function(result) {
                res.json(result)
            })
            .catch(function(err) {
                res.json({sucess: true})
            })
    });
    // update project
    server.registerService("/api/project/", "put", function(req, res) {
        var pro = JSON.stringify(req.body);
        // check projects 
        
    });

    server.registerService("/api/project/:id", "delete", function(req, res) {
        var pro = JSON.stringify(req.body);
        // check projects 
        
    });
    
    server.registerService("/api/project/:id", "get", function(req, res) {
        var pro = JSON.stringify(req.body);
        // check projects 
        
    });

    // Add user to project
    server.registerService("/api/project/:project_id/user/:id", "post", function(req, res) {
        var pro = JSON.stringify(req.body);
        // check projects 
        
    });
    // remove user from projcet
    server.registerService("/api/project/:project_id/user/:id", "delete", function(req, res) {
        var pro = JSON.stringify(req.body);
        // check projects 
        
    });

    // add user
    server.registerService("/api/user/", "post", function(req, res) {
        var pro = JSON.stringify(req.body);
        // check projects 
        
    });
    // update user
    server.registerService("/api/user/", "put", function(req, res) {
        var pro = JSON.stringify(req.body);
        // check projects 
        
    });

    // delete
    server.registerService("/api/user/:id", "delete", function(req, res) {
        var pro = JSON.stringify(req.body);
        // check projects 
        
    });
    
    // get user
    server.registerService("/api/user/:id", "get", function(req, res) {
        var pro = JSON.stringify(req.body);
        // check projects 
        
    });

    server.start();
}

dsm_restful_server_start(dsm_api_server);
